package com.hogan.hoganproject.home;

import android.os.Bundle;

import com.hogan.hoganproject.R;
import com.hogan.hoganproject.base.api.ApiConstant;
import com.hogan.hoganproject.base.fragment.BaseFragment;
import com.hogan.hoganproject.base.utils.LogUtil;
import com.hogan.hoganproject.base.view.XRecyclerView;
import com.hogan.hoganproject.home.adapter.HomeAdapter;
import com.hogan.hoganproject.home.api.JuHeBean;
import com.hogan.hoganproject.home.mvp.HomeControl;
import com.hogan.hoganproject.home.mvp.HomePresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Author: chenhu
 * Time: 2020/8/28 16:03
 * Description do somethings
 */
public class HomeFragment extends BaseFragment<HomePresenter> implements HomeControl.HomeView, XRecyclerView.RefreshAndLoadListener {
    private HomeAdapter mHomeAdapter;
    private XRecyclerView mXRecyclerView;
    private List<JuHeBean.ResultBean.DataBean> mDataBeanList = new ArrayList<>();

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutResID() {
        return R.layout.home_fragment;
    }

    @Override
    protected HomePresenter initPresenter() {
        mFragmentPresenter = new HomePresenter();
        mFragmentPresenter.onAttack(this);
        return mFragmentPresenter;
    }

    @Override
    protected void initView() {
        mXRecyclerView = mBaseView.findViewById(R.id.home_recycler_view);
        mHomeAdapter = new HomeAdapter(mDataBeanList);
        mXRecyclerView.setAdapter(mHomeAdapter);
        mXRecyclerView.setRefreshAndLoadListener(this);
       // getNewsData();
    }


    private void getNewsData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "top");
        map.put("key", ApiConstant.API_KEY);
        if (mFragmentPresenter != null) {
            mFragmentPresenter.getNewsData(map);
        }
    }

    @Override
    public void onUnsubscribe() {
        if (mFragmentPresenter != null) {
            mFragmentPresenter.onDetach();
        }
    }

    @Override
    public void onRecoverLoadData() {
        getNewsData();
    }

    @Override
    public void showDialog() {
        mLoadingDialog.show();
    }

    @Override
    public void disDialog() {
        mLoadingDialog.dismiss();
    }

    @Override
    public void updateNewsData(JuHeBean.ResultBean resultBean) {
        mDataBeanList.addAll(resultBean.getData());
        if (mHomeAdapter != null) {
            mHomeAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onLoadDataFail(String error) {
        LogUtil.e(error);
        mHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public void stopRefreshData() {
        if (mXRecyclerView != null) {
            mXRecyclerView.stopRefreshing();
        }
    }

    @Override
    public void stopLoadMoreData() {
        if (mXRecyclerView != null) {
            mXRecyclerView.stopLoadingMore();
        }
    }

    @Override
    public void startRefresh() {
        getNewsData();
    }

    @Override
    public void startLoadMore() {
        getNewsData();
    }
}
